#include "datacontroller.h"
#include <QDebug>
#include "dbfactory.h"
//    for (int i = 0; i < record.count(); ++i)
//    {
//        QVariant var = record.value(i);
//        if (var.type() == QVariant::Int)
//        {
//            qDebug() << "int";
//        }
//        else if (var.type() == QVariant::String)
//        {
//            qDebug() << "string";
//        }
//        else if (var.type() == QVariant::Double)
//        {
//            qDebug() << "double";
//        }
//        else
//        {
//            qDebug() << "other type" << var.type();
//        }
//    }
DataController::DataController(QObject *parent)
    : QObject{parent}
{
//    DBFactory::getInstance()->initDBConnection();
//    db = new DBFactory;
//    db->initDBConnection();
}

void DataController::getAllFieldFromDB()
{
    // 获取数据库所有字段
    QStringList fields = DBFactory::getInstance()->getAllField();
    emit returnAllFields(fields);

}

// 这个函数一定要分离出来，不能写在构造函数中，构造函数的执行属于主线程，数据库连接要在子线程完成
void DataController::initDBconnection(QString username, QString password)
{
//    db = new DBFactory;
    bool ok = DBFactory::getInstance()->connect(username, password);
    emit connectionState(ok);

}

void DataController::renewListItem(const QString chosenField)
{
//    qDebug() << "renew";
    QVector<QVariant> vec;
    DBFactory::getInstance()->getContentByField(chosenField, vec);
    QVector<QString> result;
    for (QVariant item: vec)
    {
        result.append(item.toString());
    }
    emit addItemToListView(result);
}

// 请求获取数据表名字
void DataController::getTableNamesFromDB()
{
    QVector<QString> tableNameList;
    DBFactory::getInstance()->getTableNames(tableNameList);
    emit returnTableNameList(tableNameList);
}

// 切换数据库当前数据表
void DataController::switchTable(const QString& newTableName)
{
    DBFactory::getInstance()->setCurrentTable(newTableName);
}

void DataController::getAllInfoFromDB()
{
    QVector<QVector<QVariant>> result;
    DBFactory::getInstance()->getAllInfo(result);
    // 传回给主线程的是一个二维字符串数组
    QVector<QVector<QString>> vec;
    for (int i = 0; i < result.size(); ++i)
    {
        QVector<QString> curRecord;
        for (int j = 0; j < result[i].size(); ++j)
        {
            curRecord.append(result[i][j].toString());
        }
        vec.append(curRecord);
    }
    emit returnAllInfo(vec);
}

void DataController::getFieldInfoFromDB(const QVector<QString> fieldList)
{
    QVector<QVector<QString>> returnedInfo;
    for (QString field: fieldList)
    {
        QVector<QVariant> vec;
        DBFactory::getInstance()->getContentByField(field, vec);
        QVector<QString> result;
        for (QVariant item: vec)
        {
            result.append(item.toString());
        }
        returnedInfo.append(result);
    }
    emit returnFieldInfo(returnedInfo);
}

void DataController::getFieldInfosFromDB(const QVector<QString> fieldList, const QVector<QVector<QString>> conditionList)
{
    QVector<QSqlRecord> result;
    DBFactory::getInstance()->getSpecialInfo(result, fieldList, conditionList);
    QVector<QVector<QString>> infos;
    for (int i = 0; i < result.size(); ++i)
    {
        QVector<QString> info;
        for (int fieldIndex = 0; fieldIndex < result[i].count(); ++fieldIndex)
        {
            info.append(result[i].value(fieldIndex).toString());
        }
        infos.append(info);
    }
//    qDebug() << "infos:" << infos;
    emit returnNewTableShowInfo(infos);
}
